package com.demo;

import java.sql.*;
import java.util.Collection;

public class 处理查询结果集 {
    public static void main(String[] args) {
        Connection con = null;
        Statement sta = null;
        ResultSet res = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取链接数据库
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xclmf", "root", "552200");
            System.out.println("链接的数据库是：" + con);
            //获取数据库操作对象
            sta = con.createStatement();
            //执行sql语句
            String str = "select * from books_4";
            res = sta.executeQuery(str);

            /**处理查询结果集
             //            System.out.println("查询方式一：如下");
             //            System.out.println("----------------------------------------------------");
             //            System.out.println("|  "+"id"+"  |  "+"bName"+"  |  "+"price"+"  |  "+"author"+"  |  "+"datetime"+"  |");
             //            while(res.next()){
             //                String id=res.getString(1);
             //                String bName=res.getString(2);
             //                String price=res.getString(3);
             //                String author=res.getString(4);
             //                String datetime=res.getString(5);
             //                System.out.println("|  "+id+"  |  "+bName+"  |  "+price+"  |  "+author+"  |  "+datetime+"  |");
             //            }
             //            System.out.println("----------------------------------------------------");


             //            System.out.println("查询方式二：如下");
             //            System.out.println("----------------------------------------------------");
             //            System.out.println("|  "+"id"+"  |  "+"bName"+"  |  "+"price"+"  |  "+"author"+"  |  "+"datetime"+"  |");
             //            while(res.next()){
             //                String id=res.getString("id");
             //                String bName=res.getString("bName");
             //                String price=res.getString("price");
             //                String author=res.getString("authorId");
             //                String datetime=res.getString("publishDate");
             //                System.out.println("|  "+id+"  |  "+bName+"  |  "+price+"  |  "+author+"  |  "+datetime+"  |");
             //            }
             //            System.out.println("----------------------------------------------------");
             */
            System.out.println("查询方式三：如下");
            System.out.println("----------------------------------------------------");
            System.out.println("|  " + "id" + "  |  " + "bName" + "  |  " + "price" + "  |  " + "author" + "  |  " + "datetime" + "  |");
            System.out.println("----------------------------------------------------");

            while (res.next()) {
                int id = res.getInt(1) + 1;
                String bName = res.getString(2);
                double price = res.getDouble(3) + 20.0;
                String author = res.getString(4);
                Date datetime = res.getDate(5);

                System.out.println("|  " + id + "  |  " + bName + "  |  " + price + "  |  " + author + "  |  " + datetime + "  |");
                System.out.println("----------------------------------------------------");


            }
            System.out.println("----------------------------------------------------");
            System.out.println("执行命令成功！");

        } catch (Exception e) {
            e.printStackTrace();
//            System.out.println("代码出错！");
        } finally {
            //释放资源
            if (res != null) {
                try {
                    res.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (sta != null) {
                try {
                    sta.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }

    }
}
